﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: StoppingCriteria.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2013-01-24 4:34 PM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;

namespace BOL.Algorithms
{
    public interface IStoppingCriterion
    {
        dynamic Criterion { get; }
        Predicate<dynamic> IsSatisfied { get; }
    }

    public abstract class StoppingCriterionBase : IStoppingCriterion
    {
        public dynamic Criterion { get; private set; }
        public abstract Predicate<dynamic> IsSatisfied { get; }

        protected StoppingCriterionBase(dynamic criterion)
        {
            Criterion = criterion;
        }
    }

    public class IterationStoppingCriterion : StoppingCriterionBase
    {
        public IterationStoppingCriterion(int maxIterations)
            : base(maxIterations) { }

        public override Predicate<dynamic> IsSatisfied
        {
            get { return i => i > Criterion; }
        }
    }

    public class ThresholdStoppingCriterion : StoppingCriterionBase
    {
        public ThresholdStoppingCriterion(double threshold)
            : base(threshold) { }

        public override Predicate<dynamic> IsSatisfied
        {
            get { return t => t <= Criterion; }
        }
    }
}
